
package com.insigma.business.qrcode.common.converter;

import com.insigma.business.qrcode.common.dto.BaseDO;
import com.insigma.business.qrcode.common.vo.BaseDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;


public abstract class AbstractConverter<DTO extends BaseDTO, DOEntity extends BaseDO> {

    protected static final Logger logger = LoggerFactory.getLogger(
            AbstractConverter.class);

    
    protected abstract DTO doConvertResponseDO2DTO(DOEntity doEntity);

    
    public List<DTO> convertGenericResponseDOList2DTOList(final List<DOEntity> doList){
        List<DTO> viewObjList = new ArrayList<>();
        for(DOEntity doEntity: doList){
            viewObjList.add(convertGenericResponseDO2DTO(doEntity));
        }
        return viewObjList;
    }

    
    public DTO convertGenericResponseDO2DTO(final DOEntity doEntity){
        try {
            if (doEntity == null) {
                return null;
            }
            return doConvertResponseDO2DTO(doEntity);
        } catch (Exception e) {
            logger.error(MessageFormat.format("转换模型{0}过程发生错误", doEntity), e);
            throw new RuntimeException(MessageFormat.format("转换模型{0}过程发生错误", doEntity), e);
        }
    }

}
